﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;

namespace ExamServer.Forms
{
    public partial class DialogSetting : Form
    {
        public DialogSetting()
        {
            InitializeComponent();
        }

        private void DialogSetting_Load(object sender, EventArgs e)
        {
            textBox4.Text = Properties.Settings.Default.ListenPort.ToString();
            textBox6.Text = Properties.Settings.Default.ConnectionNumber.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox4.Text.Trim() != "" && textBox4.Text.Trim() != Properties.Settings.Default.ListenPort.ToString())
            {
                Properties.Settings.Default.ListenPort = Convert.ToInt32(textBox4.Text.Trim());
                Properties.Settings.Default.Save();
            }
            if (textBox6.Text.Trim() != "" && textBox6.Text.Trim() != Properties.Settings.Default.ConnectionNumber.ToString())
            {
                Properties.Settings.Default.ConnectionNumber = Convert.ToInt32(textBox6.Text.Trim());
                Properties.Settings.Default.Save();
            }
            if (textBox1.Text.Trim() != "" && textBox2.Text.Trim() != "" && textBox3.Text.Trim() != "" && textBox5.Text.Trim() != "")
            {
                string str = "Data Source=" + textBox1.Text.Trim() + ";Initial Catalog=" + textBox5.Text.Trim() + ";Persist Security Info=True;User ID=" + textBox2.Text.Trim() + ";Password=" + textBox3.Text.Trim();
                Properties.Settings.Default.ConnectionString = DES.EncryptDES(str, "de_a3450");
                Properties.Settings.Default.Save();
            }
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                string str = "Data Source=" + textBox1.Text.Trim() + ";Initial Catalog=" + textBox5.Text.Trim() + ";Persist Security Info=True;User ID=" + textBox2.Text.Trim() + ";Password=" + textBox3.Text.Trim();
                DBConnection conn = new DBConnection(str);
                conn.GetConnection().Open();
                conn.GetConnection().Close();
            }
            catch (Exception)
            {
                MessageBox.Show(this, "连接失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            MessageBox.Show(this, "连接可用！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                string HostName = Dns.GetHostName(); //得到主机名
                IPHostEntry IpEntry = Dns.GetHostEntry(HostName); //得到主机IP
                IPAddress[] addrs = IpEntry.AddressList;
                IPAddress ipAddress = null;
                foreach (IPAddress ip in addrs)
                {
                    if (ip.AddressFamily == AddressFamily.InterNetwork)
                    {
                        ipAddress = IPAddress.Parse(ip.ToString());
                    }
                }
                IPEndPoint localEndPoint = new IPEndPoint(ipAddress, Convert.ToInt32(textBox4.Text.Trim()));
                Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                s.Bind(localEndPoint);
                s.Close();
            }
            catch(Exception)
            {
                MessageBox.Show(this, "端口不可用！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            MessageBox.Show(this, "端口可以使用！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }
    }
}
